package com.tao.orders.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
import org.springframework.security.web.SecurityFilterChain;

/**
 * <p>
 *
 * </p>
 *
 * @author taohongrun
 * @since 2024/11/21
 */
@Configuration
@EnableWebSecurity
public class ResourceServerConfig {

    @Autowired
    JwtAuthenticationConverter jwtAuthenticationConverter;

    public void SecurityConfig(JwtAuthenticationConverter jwtAuthenticationConverter) {
        this.jwtAuthenticationConverter = jwtAuthenticationConverter;
    }

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                .oauth2ResourceServer(oauth2 -> oauth2
                        .jwt(jwt -> jwt
                                .jwtAuthenticationConverter(jwtAuthenticationConverter)
                        )
                )
                .authorizeHttpRequests(authorize -> authorize
                        // 全部放行
                        .anyRequest().permitAll()
                )
                // 禁用 CSRF 保护（如果需要）
                .csrf(csrf -> csrf.disable());

        return http.build();
    }
}

